/**
 * XL-Payment 工具函数
 */

/**
 * 生成唯一订单ID
 * @returns {String} 订单ID
 */
export function generateOrderId() {
  const timestamp = new Date().getTime();
  const random = Math.floor(Math.random() * 1000000).toString().padStart(6, '0');
  return `${timestamp}${random}`;
}

/**
 * 格式化金额
 * @param {Number|String} amount 金额
 * @returns {Number} 格式化后的金额，保留两位小数
 */
export function formatAmount(amount) {
  if (typeof amount === 'string') {
    amount = parseFloat(amount);
  }
  
  if (isNaN(amount)) {
    return 0;
  }
  
  // 保留两位小数
  return Math.round(amount * 100) / 100;
}

/**
 * 合并配置
 * @param {Object} defaultConfig 默认配置
 * @param {Object} userConfig 用户配置
 * @returns {Object} 合并后的配置
 */
export function mergeConfig(defaultConfig, userConfig) {
  if (!userConfig) {
    return { ...defaultConfig };
  }
  
  const result = { ...defaultConfig };
  
  for (const key in userConfig) {
    if (Object.prototype.hasOwnProperty.call(userConfig, key)) {
      const value = userConfig[key];
      
      // 如果是对象且不是数组，则递归合并
      if (value && typeof value === 'object' && !Array.isArray(value) && 
          result[key] && typeof result[key] === 'object' && !Array.isArray(result[key])) {
        result[key] = mergeConfig(result[key], value);
      } else {
        result[key] = value;
      }
    }
  }
  
  return result;
}

/**
 * 延迟函数
 * @param {Number} ms 延迟毫秒数
 * @returns {Promise} Promise对象
 */
export function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

/**
 * 格式化日期
 * @param {Date|String|Number} date 日期对象或时间戳
 * @param {String} format 格式化模板
 * @returns {String} 格式化后的日期字符串
 */
export function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
  if (!date) {
    return '';
  }
  
  // 转换为日期对象
  if (typeof date === 'string' || typeof date === 'number') {
    date = new Date(date);
  }
  
  if (!(date instanceof Date) || isNaN(date.getTime())) {
    return '';
  }
  
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();
  
  // 填充为两位数
  const pad = num => num.toString().padStart(2, '0');
  
  return format
    .replace(/YYYY/g, year)
    .replace(/MM/g, pad(month))
    .replace(/DD/g, pad(day))
    .replace(/HH/g, pad(hours))
    .replace(/mm/g, pad(minutes))
    .replace(/ss/g, pad(seconds));
}

/**
 * 获取当前平台标识
 * @returns {String} 平台标识
 */
export function getPlatform() {
  // #ifdef APP-PLUS
  return 'app';
  // #endif
  
  // #ifdef MP-WEIXIN
  return 'mp-weixin';
  // #endif
  
  // #ifdef MP-ALIPAY
  return 'mp-alipay';
  // #endif
  
  // #ifdef H5
  return 'h5';
  // #endif
  
  return 'unknown';
}

/**
 * 解析货币代码
 * @param {String} currency 货币代码 
 * @returns {Object} 货币信息
 */
export function parseCurrency(currency = 'CNY') {
  const currencyMap = {
    CNY: { symbol: '¥', name: '人民币', decimals: 2 },
    USD: { symbol: '$', name: '美元', decimals: 2 },
    EUR: { symbol: '€', name: '欧元', decimals: 2 },
    GBP: { symbol: '£', name: '英镑', decimals: 2 },
    JPY: { symbol: '¥', name: '日元', decimals: 0 },
    HKD: { symbol: 'HK$', name: '港币', decimals: 2 },
    KRW: { symbol: '₩', name: '韩元', decimals: 0 },
    TWD: { symbol: 'NT$', name: '新台币', decimals: 2 },
    SGD: { symbol: 'S$', name: '新加坡元', decimals: 2 },
    AUD: { symbol: 'A$', name: '澳元', decimals: 2 },
    CAD: { symbol: 'C$', name: '加元', decimals: 2 },
    MYR: { symbol: 'RM', name: '马来西亚林吉特', decimals: 2 },
    THB: { symbol: '฿', name: '泰铢', decimals: 2 },
    VND: { symbol: '₫', name: '越南盾', decimals: 0 },
    PHP: { symbol: '₱', name: '菲律宾比索', decimals: 2 },
    IDR: { symbol: 'Rp', name: '印尼盾', decimals: 0 },
    INR: { symbol: '₹', name: '印度卢比', decimals: 2 },
    RUB: { symbol: '₽', name: '俄罗斯卢布', decimals: 2 },
    AED: { symbol: 'د.إ', name: '阿联酋迪拉姆', decimals: 2 },
    SAR: { symbol: '﷼', name: '沙特里亚尔', decimals: 2 },
    BTC: { symbol: '₿', name: '比特币', decimals: 8 },
    ETH: { symbol: 'Ξ', name: '以太币', decimals: 18 },
    USDT: { symbol: '₮', name: 'Tether', decimals: 6 }
  };
  
  return currencyMap[currency] || { symbol: currency, name: currency, decimals: 2 };
}

/**
 * 格式化金额显示
 * @param {Number} amount 金额 
 * @param {String} currency 货币代码
 * @returns {String} 格式化后的金额字符串
 */
export function formatCurrency(amount, currency = 'CNY') {
  const currencyInfo = parseCurrency(currency);
  const { symbol, decimals } = currencyInfo;
  
  // 根据货币小数点位数格式化
  const formattedAmount = formatAmount(amount).toFixed(decimals);
  
  return `${symbol}${formattedAmount}`;
} 